package com.zr.action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.Content;
import com.zr.service.ContentService;
import com.zr.service.impl.ContentServiceImpl;

public class GetContentByCidAction extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		HttpSession session = req.getSession();
		int cid = (int)session.getAttribute("contnet_cid");
		ContentService cs = new ContentServiceImpl();
		Content content = cs.getContentByCid(cid);
		String articl = content.getCcontent();
		StringBuffer essay = new StringBuffer();
		int i = 0;
		int n = 0;
		while(i<(int)Math.ceil((double)articl.length()/30)){
			if(i==0)
			{
				essay.append(articl.substring(n, n+30<articl.length()?n+28:articl.length()));
				n=n+28;
			}else
			{
				essay.append(articl.substring(n, n+30<articl.length()?n+30:articl.length()));
				n=n+30;
			}
			essay.append("<br>");
			i++;
		}
		String cname = content.getCname();
		ArrayList<Content> list = cs.getRelevantContentByCname(cname,cid);
		session.setAttribute("list", list);
		session.setAttribute("content", essay.toString());
		session.setAttribute("date", content.getCtime());
		session.setAttribute("cnumber", content.getCnumber());
		session.setAttribute("title", content.getCtitle());
		req.getRequestDispatcher("personPage.jsp").forward(req, resp);
	}
}